import java.util.Arrays;
import java.util.PriorityQueue;

public class TestHeap {
    private int[] elum;
    private int usedsize;

    public TestHeap() {
        this.elum = new int[10];
    }

    public void initHeap(int[] arr) {
        //先把数组导进去
        for (int i = 0; i < arr.length; i++) {
            elum[i] = arr[i];
            this.usedsize++;
        }
    }

    public void creatHeap() {
        //先找到最后一棵子树的根节点
        int parent = (elum.length - 1 - 1) / 2;
        //循环向前找到每棵子树的根节点
        for (int i = parent; i >= 0; i--) {
            shiftDown(i, usedsize);
        }
    }

    //向下调整
    private void shiftDown(int parent, int usedsize) {
        //先根据根节点找到左子节点
        int child = (parent * 2) + 1;
        //先判断子节点要在usedsize范围内
        while (child < usedsize) {
            //判断左右子结点谁大，同时防止右子节点越界
            if (child + 1 < usedsize && elum[child] < elum[child + 1]) {
                child++;
            }

            if (elum[child] > elum[parent]) {
                swap(parent, child);
                parent = child;
                child = 2 * parent + 1;
            } else {
                break;
            }
        }
    }

    //插入元素
    public void offer(int val) {
        if (isFull()) {
            this.elum = Arrays.copyOf(elum, elum.length * 2);

        }
        //插入到最后的位置
        elum[usedsize] = val;
        //向上调整
        shiftUp(usedsize);


    }

    //向上调整
    private void shiftUp(int child) {
        //获取根节点
        int parent = (child - 1) / 2;

        while (child >= 0) {
            if (elum[child] > elum[parent]) {
                swap(child, parent);
                child = parent;
                parent = (child - 1) / 2;
            } else {
                break;
            }
        }
    }

    //删除元素
    public int poll() {
        int tmp = elum[0];
        swap(0, usedsize - 1);
        usedsize--;
        shiftDown(0, usedsize);
        return tmp;
    }

    private boolean isFull() {
        return usedsize == elum.length;
    }

    private void swap(int parent, int child) {
        int tmp = elum[parent];
        elum[parent] = elum[child];
        elum[child] = tmp;
    }

    //前k个最大的数
    public int[] maxLestk(int[] arr, int k) {
        int[] ret = new int[k];
        if (arr == null || k <= 0) {
            return ret;
        }
        //1.创建一个数量为k的小根堆
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
        for (int i = 0; i < k; i++) {
            priorityQueue.offer(arr[i]);

        }
        //2.剩下元素逐个与堆顶比较
        for (int i = k; i < arr.length; i++) {
            //2.1比堆顶大的出堆顶，把另外的元素offer进来
            if (priorityQueue.peek()<arr[i]){
                priorityQueue.poll();
                priorityQueue.offer(arr[i]);
            }
        }
        for (int i=0;i<k;i++){
            ret[i]= priorityQueue.poll();
        }
        return ret;
    }


}
